package org.jeecg.common.system.query;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.domain.QueryRequest;

import java.util.stream.IntStream;

/**
 * 处理排序工具类
 *
 * @author CHEaN
 * @date 2021/11/15
 */
public class PageGenerator {

    private PageGenerator() {
    }

    /**
     * 处理排序（分页情况下） for mybatis-plus
     *
     * @param request           QueryRequest
     * @param page              Page<T>
     * @param defaultSort       默认排序的字段
     * @param defaultOrder      默认排序规则
     * @param camelToUnderscore 是否开启驼峰转下划线
     */
    public static <T> void handlePageSort(QueryRequest request, Page<T> page, String defaultSort, Boolean defaultOrder, Boolean camelToUnderscore) {
        // 设置分页页数与页码
        page.setCurrent(request.getPageNo());
        page.setSize(request.getPageSize());
        // 排序字段
        String sortField = request.getColumn();
        if (Boolean.TRUE.equals(camelToUnderscore)) {
            sortField = camelToUnderscore(sortField);
            defaultSort = camelToUnderscore(defaultSort);
        }
        // 排序工厂
        OrderItem orderItem = new OrderItem();
        if (StringUtils.isNotBlank(request.getColumn())
                && !StringUtils.equalsIgnoreCase(request.getColumn(), CommonConstant.UNDEFINED)) {
            orderItem.setColumn(sortField);
        } else if (StringUtils.isNotBlank(defaultSort)) {
            orderItem.setColumn(defaultSort);
        } else {
            orderItem.setColumn(CommonConstant.DEFAULT_ORDER_COLUMN);
        }
        if ("desc".equalsIgnoreCase(request.getOrder())) {
            orderItem.setAsc(Boolean.FALSE);
        } else if ("asc".equalsIgnoreCase(request.getOrder())) {
            orderItem.setAsc(Boolean.TRUE);
        } else if (defaultOrder != null) {
            orderItem.setAsc(defaultOrder);
        } else {
            orderItem.setAsc(Boolean.FALSE);
        }
        page.addOrder(orderItem);
    }

    /**
     * 处理排序 for mybatis-plus
     *
     * @param request QueryRequest
     * @param page    Page
     */
    public static <T> void handlePageSort(QueryRequest request, Page<T> page) {
        handlePageSort(request, page, null, null, Boolean.FALSE);
    }

    /**
     * 处理排序 for mybatis-plus
     *
     * @param request           QueryRequest
     * @param page              Page<T>
     * @param camelToUnderscore 是否开启驼峰转下划线
     */
    public static <T> void handlePageSort(QueryRequest request, Page<T> page, Boolean camelToUnderscore) {
        handlePageSort(request, page, null, null, camelToUnderscore);
    }

    /**
     * 创建排序 for mybatis-plus
     *
     * @param request           QueryRequest
     * @param defaultSort       默认排序的字段
     * @param defaultOrder      默认排序规则
     * @param camelToUnderscore 是否开启驼峰转下划线
     * @return Page<T>
     */
    public static <T> Page<T> createPageSort(QueryRequest request, String defaultSort, Boolean defaultOrder, Boolean camelToUnderscore) {
        Page<T> page = new Page<>();
        handlePageSort(request, page, defaultSort, defaultOrder, camelToUnderscore);
        return page;
    }

    /**
     * 创建排序 for mybatis-plus
     *
     * @param request      QueryRequest
     * @param defaultSort  默认排序的字段
     * @param defaultOrder 默认排序规则
     * @return Page<T>
     */
    public static <T> Page<T> createPageSort(QueryRequest request, String defaultSort, Boolean defaultOrder) {
        Page<T> page = new Page<>();
        handlePageSort(request, page, defaultSort, defaultOrder, Boolean.FALSE);
        return page;
    }

    /**
     * 创建排序 for mybatis-plus
     *
     * @param request           QueryRequest
     * @param camelToUnderscore 是否开启驼峰转下划线
     * @return Page<T>
     */
    public static <T> Page<T> createPageSort(QueryRequest request, Boolean camelToUnderscore) {
        Page<T> page = new Page<>();
        handlePageSort(request, page, null, null, camelToUnderscore);
        return page;
    }

    /**
     * 创建排序 for mybatis-plus
     *
     * @param request QueryRequest
     * @return Page<T>
     */
    public static <T> Page<T> createPageSort(QueryRequest request) {
        Page<T> page = new Page<>();
        handlePageSort(request, page, null, null, Boolean.TRUE);
        return page;
    }

    /**
     * 驼峰转下划线
     *
     * @param value 待转换值
     * @return 返回信息
     */
    public static String camelToUnderscore(String value) {
        if (StringUtils.isBlank(value))
            return value;
        String[] arr = StringUtils.splitByCharacterTypeCamelCase(value);
        if (arr.length == 0)
            return value;
        StringBuilder result = new StringBuilder();
        IntStream.range(0, arr.length).forEach(i -> {
            if (i != arr.length - 1)
                result.append(arr[i]).append(StringPool.UNDERSCORE);
            else
                result.append(arr[i]);
        });
        return StringUtils.lowerCase(result.toString());
    }

}
